package com.ming.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.reggie.dto.SetmealDto;
import com.ming.reggie.entity.Category;
import com.ming.reggie.entity.Page;
import com.ming.reggie.entity.Setmeal;
import com.ming.reggie.entity.SetmealDish;
import com.ming.reggie.exception.BusinessException;
import com.ming.reggie.mapper.CategoryMapper;
import com.ming.reggie.mapper.SetmealDishMapper;
import com.ming.reggie.mapper.SetmealMapper;
import com.ming.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 套餐的分页查询数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);

        List<Setmeal> setmealList = setmealMapper.findAllByName(name);

        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmealList);

        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();

            //将setmeal套餐对象的数据赋值给setmealdto里面的数据
            BeanUtils.copyProperties(setmeal,setmealDto);
            //获取类别名称
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            //将类别名称赋值给setmealDto
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        //5封装钱队需要Page<setmealDto>对象
        Page<SetmealDto> dtoPage = new Page<>();
        dtoPage.setRecords(setmealDtoList);
        dtoPage.setTotal(setmealPageInfo.getTotal());

        return dtoPage;
    }

    /**
     * 添加套餐的业务方法
     *
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public boolean save(SetmealDto setmealDto) {
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        setmealMapper.save(setmealDto);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());

        return setmealDishMapper.save(setmealDishList)>0;
    }

    /**
     * 删除套餐的业务方法
     * @param ids 要删除的套餐id列表数据
     * @param updateuser 更新人
     * @return
     */
    @Transactional
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateuser) {
        Integer count = setmealMapper.countByIds(ids);
        if(count>0){
            throw new BusinessException("无法删除起售的套餐");
        }
        //批量删除套餐菜品关系菜品
        setmealDishMapper.deleteBySetmealIds(ids,updateuser,LocalDateTime.now());
        //批量删除套餐
        setmealMapper.deleteByIds(ids,updateuser,LocalDateTime.now());
        return true;
    }
}
